/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package com.mcx.modules.service.impl;

import com.mcx.modules.domain.Rechargeproduct;
import lombok.RequiredArgsConstructor;
import com.mcx.modules.repository.RechargeproductRepository;
import com.mcx.modules.service.RechargeproductService;
import com.mcx.modules.service.dto.RechargeproductDto;
import com.mcx.modules.service.dto.RechargeproductQueryCriteria;
import com.mcx.modules.service.mapstruct.RechargeproductMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import com.mcx.utils.PageUtil;
import com.mcx.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author mcx
* @date 2022-04-30
**/
@Service
@RequiredArgsConstructor
public class RechargeproductServiceImpl implements RechargeproductService {

    private final RechargeproductRepository rechargeproductRepository;
    private final RechargeproductMapper rechargeproductMapper;

    @Override
    public Map<String,Object> queryAll(RechargeproductQueryCriteria criteria, Pageable pageable){
        Page<Rechargeproduct> page = rechargeproductRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(rechargeproductMapper::toDto));
    }

    @Override
    public List<RechargeproductDto> queryAll(RechargeproductQueryCriteria criteria){
        return rechargeproductMapper.toDto(rechargeproductRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public RechargeproductDto findById(Integer id) {
        Rechargeproduct rechargeproduct = rechargeproductRepository.findById(id).orElseGet(Rechargeproduct::new);
        return rechargeproductMapper.toDto(rechargeproduct);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RechargeproductDto create(Rechargeproduct resources) {
        return rechargeproductMapper.toDto(rechargeproductRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(Rechargeproduct resources) {
        Rechargeproduct rechargeproduct = rechargeproductRepository.findById(resources.getId()).orElseGet(Rechargeproduct::new);
        rechargeproduct.copy(resources);
        rechargeproductRepository.save(rechargeproduct);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            rechargeproductRepository.deleteById(id);
        }
    }
}